package com.onetech.adrewards.free.fragments;

import java.util.List;

import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.adapters.ListNewsAdapter;
import com.onetech.adrewards.free.customviews.LoadMoreListView;
import com.onetech.adrewards.free.models.NewsModel;
import com.onetech.adrewards.free.objects.NewsDTO;

public class ListNewsFragment extends BaseHomeChildFragment implements
		OnItemClickListener {
	private ListNewsAdapter mAdapter;
	private GetListNews mGetListNews = null;
	private View mFeedbackBtn = null;

	private View mCurrentViewSelected = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mView == null) {
			mView = inflater.inflate(R.layout.fragment_list_news_layout,
					container, false);
			initView();
			initListener();
		} else {
			((ViewGroup) mView.getParent()).removeView(mView);
		}
		return mView;
	}

	private void initView() {
		mListView = (LoadMoreListView) mView.findViewById(R.id.myNewsListView);
		mListView.setVisibility(View.GONE);
		mAdapter = new ListNewsAdapter(getActivity());
		mListView.setAdapter(mAdapter);
		mFeedbackBtn = (RelativeLayout) mView.findViewById(R.id.feedbackBtn);
		initNotFoundAndLoadingView();
	}

	private void initListener() {
		mFeedbackBtn.setOnClickListener(this);
		initRetryButtonListener();
		mListView.setOnLoadMoreListener(this);
		mListView.setOnItemClickListener(this);
	}

	@Override
	public void onLoadMore() {
		super.onLoadMore();
		getListNews();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.feedbackBtn:
			// Intent mFeedbackIntent=new Intent(getActivity(),
			// FeedbackOptionActivity.class);
			// startActivity(mFeedbackIntent);
			getBaseNewActivity().onNavigationDrawerItemSelected(11);
			break;
		case R.id.btnRetry:
			pageNo = 1;
			getListNews();
			break;
		default:
			super.onClick(v);
			break;
		}

	}

	@Override
	public void onResume() {
		super.onResume();
		if (mAdapter != null && mAdapter.getCount() < 1) {
			getListNews();
		}
	}

	private void getListNews() {
		if (mGetListNews == null || mGetListNews.getStatus() == Status.FINISHED) {
			mGetListNews = new GetListNews();
			mGetListNews.execute();
		}
	}

	private class GetListNews extends AsyncTask<Void, Void, List<NewsDTO>> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showNotFoundOrLoadingView(false, true);
		}

		@Override
		protected List<NewsDTO> doInBackground(Void... params) {
			return NewsModel.getInstance().getListNews(String.valueOf(pageNo));
		}

		@Override
		protected void onPostExecute(List<NewsDTO> result) {
			super.onPostExecute(result);
			if (result != null && result.size() > 0) {
				if (pageNo == 1) {
					mAdapter.updateAppList(result);
				} else {
					mAdapter.insertAppList(result);
				}
				pageNo++;
			}
			if (mAdapter.getCount() < 1) {
				showNotFoundOrLoadingView(false, false);
				mListView.setVisibility(View.GONE);
			} else {
				showNotFoundOrLoadingView(false, false);
				mListView.setVisibility(View.VISIBLE);
			}
			mListView.onLoadMoreComplete();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		try {
			if (position > mAdapter.getCount() - 1) {
				return;
			}
			NewsDTO item = mAdapter.getItem(position);
			if (item == null) {
				return;
			}
			if (mCurrentViewSelected != null) {
				mCurrentViewSelected.setBackgroundResource(R.drawable.item_list_new_background_normal);
			}
			
			mCurrentViewSelected = view;

			TextView mContent = (TextView) view.findViewById(R.id.itemDescriptionNews);
			ImageView mIconNavi = (ImageView) view.findViewById(R.id.newsImage);
			item.setIsToogle(item.IsToogle() ? false : true);
			if (!item.IsToogle()) {
				if (mContent.getLineCount() > 2) {
					mIconNavi.setVisibility(View.GONE);
					view.findViewById(R.id.lineListNews).setVisibility(View.INVISIBLE);	
					view.setBackgroundResource(R.drawable.item_list_new_background_selected);
				}						
				mContent.setLines(mContent.getLineCount());
			} else {
				// mContent.setSingleLine(item.IsToogle());
				if (mContent.getLineCount() > 2) {
					mIconNavi.setVisibility(View.VISIBLE);
					mContent.setLines(2);
				} else {
					if (mContent.getLineCount() == 2) {
						mContent.setLines(2);
					} else {
						mContent.setLines(1);
					}
				}
				view.setBackgroundResource(R.drawable.item_list_new_background_normal);
				view.findViewById(R.id.lineListNews).setVisibility(View.VISIBLE);
			}

			// NewsDetailDialog mNewsDetialDialog=new
			// NewsDetailDialog(getActivity(), item);
			// mNewsDetialDialog.show();
		} catch (Exception e) {
			return;
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		try {
			mTracker.set(Fields.SCREEN_NAME,
					getActivity().getString(R.string.ListNewsFragment));
			mTracker.send(MapBuilder.createAppView().build());
		} catch (Exception e) {
		}
	}
}
